//
//  LXAlertView.m
//  bgms_d
//
//  Created by lachesis-zhu on 2019/2/26.
//  Copyright © 2019 lachesis-zhu. All rights reserved.
//

#import "LXAlertView.h"
#import <AudioToolbox/AudioToolbox.h>
#import "LXDevSettingModel.h"
@interface LXAlertView () <AVAudioPlayerDelegate>
{
    SystemSoundID sound;
}
@property (nonatomic,strong)NSTimer *_vibrationTimer;
@end

@implementation LXAlertView
@synthesize _vibrationTimer;

- (void)awakeFromNib {
    [super awakeFromNib];
    self.roundHeight.constant = self.boardView.lx_width / 2.5;
    self.bottomLength.constant = - self.boardView.lx_width / 5;
    
    self.boardView.layer.cornerRadius = 4;
    self.boardView.layer.masksToBounds = YES;
    
    self.roundView.layer.cornerRadius = self.boardView.lx_width / 5;
    self.roundView.layer.masksToBounds = YES;
    self.roundView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.roundView.layer.borderWidth = 5;
    
    self.button1.layer.cornerRadius = 5;
    self.button1.layer.masksToBounds = YES;
    self.button2.layer.cornerRadius = 5;
    self.button2.layer.masksToBounds = YES;
    
    self.titleLable2.numberOfLines = 2;
    self.titleLable2.lineBreakMode = NSLineBreakByTruncatingTail;
    self.detailLabel.numberOfLines = 2;
    self.detailLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    
    [self alertRangton];
}

- (IBAction)btnDown1:(id)sender {
    [self removeFromSuperview];
    [self stopShakeSound];
    [self.delegate alertView:self alertDown:sender];
}

- (IBAction)btnDown2:(id)sender {
    [self stopShakeSound];
    [self removeFromSuperview];
    [self.delegate alertView:self okDown:sender];
}

- (void)alertHidden {
    [self stopShakeSound];
    [self removeFromSuperview];
}

- (void)alertRangton
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sounds" ofType:@"mp3"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &sound);
    AudioServicesAddSystemSoundCompletion(sound, NULL, NULL, soundCompleteCallback, NULL);
}

- (void)alertSounds
{
    if ([LXDevSettingModel shareSettingModel].alertRingtone) {
        AudioServicesPlaySystemSound(sound);
    }
    
    if ([LXDevSettingModel shareSettingModel].alertVibration) {
        _vibrationTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(playkSystemSound) userInfo:nil repeats:YES];
    }
}

//振动
- (void)playkSystemSound {
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

//停止响铃及振动
-(void)stopShakeSound {
    [_vibrationTimer invalidate];
    AudioServicesRemoveSystemSoundCompletion(sound);
    AudioServicesDisposeSystemSoundID(sound);
}
//响铃回调方法
void soundCompleteCallback(SystemSoundID sound,void * clientData) {
    AudioServicesPlaySystemSound(sound);
}
@end
